package com.lemon.components.ant.swagger.makeui;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.swagger.makeui.dto.AntdMakerReq;
import com.lemon.components.ant.swagger.makeui.dto.SwaggerRequest;
import com.lemon.components.ant.swagger.makeui.dto.UIConfig;
import com.lemon.components.ant.swagger.net.DocBase;
import com.lemon.components.ant.swagger.net.dto.GetBaseDocReq;
import com.lemon.components.ant.swagger.net.dto.GetBaseDocRsp;
import com.lemon.components.ant.swagger.net.enums.HttpMethod;
import com.lemon.components.ant.util.FileUtil;
import com.lemon.components.ant.util.TemplateUtil;

import lombok.experimental.UtilityClass;

@UtilityClass
public class AntdFromSwaggerMaker {
	
	private final String fetchjs = "D:/workspace/xw-boss-h5/src/common/fetch.js";
	
	public static void main(String[] args) throws Exception {
		UIConfig config = UIConfig.builder()
				.新增(new SwaggerRequest("/boss/merchant-pay/merchant-pay-add"))
				.删除(new SwaggerRequest("/boss/merchant-pay/merchant-pay-delete"))
				.编辑(new SwaggerRequest("/boss/merchant-pay/merchant-pay-edit"))
				.详情(new SwaggerRequest("/boss/merchant-pay/merchant-pay-get",HttpMethod.get))
				.表格列表(new SwaggerRequest("/boss/merchant-pay/merchant-pay-list"))
				.keyField("wepayAccountId")
				.build();
		
		
		AntdMakerReq req = AntdMakerReq.builder()
				.config(config)
				.serviceName("merchantWechatpayAccount")
				.serviceDescription("支付账户管理")
				.vueModelName("settings")
				.fetchjs(true)
				.build();
		AntdFromSwaggerMaker.make(req);
	}
	
	public void make(AntdMakerReq req) throws Exception {
		
		GetBaseDocRsp result;
		if(req.getConfig().get表格列表()!=null) {
			result = DocBase.getBaseDocs(GetBaseDocReq.builder()
					.docBaseUrl("https://xw.tst.cqtancheng.com/v3/api-docs?group=tanqiqi")
					.apiUrl(req.getConfig().get表格列表().getUrl())
					.isTableResult(true)
					.method(req.getConfig().get表格列表().getMethod()).build());
			req.setTableData(result);
		}
		
		if(req.getConfig().get编辑()!=null) {
			result = DocBase.getBaseDocs(GetBaseDocReq.builder()
					.docBaseUrl("https://xw.tst.cqtancheng.com/v3/api-docs?group=tanqiqi")
					.apiUrl(req.getConfig().get编辑().getUrl())
					.isTableResult(false)
					.method(req.getConfig().get编辑().getMethod()).build());
			req.setUpdateOpt(result);
		}
		
		if(req.getConfig().get新增()!=null) {
			result = DocBase.getBaseDocs(GetBaseDocReq.builder()
					.docBaseUrl("https://xw.tst.cqtancheng.com/v3/api-docs?group=tanqiqi")
					.apiUrl(req.getConfig().get新增().getUrl())
					.isTableResult(false)
					.method(req.getConfig().get新增().getMethod()).build());
			req.setAddOpt(result);
		}
		
		if(req.getConfig().get详情()!=null) {
			result = DocBase.getBaseDocs(GetBaseDocReq.builder()
					.docBaseUrl("https://xw.tst.cqtancheng.com/v3/api-docs?group=tanqiqi")
					.apiUrl(req.getConfig().get详情().getUrl())
					.isTableResult(false)
					.method(req.getConfig().get详情().getMethod()).build());
			req.setDetailOpt(result);
		}
		
		TemplateUtil.getInstance(AntdFromSwaggerMaker.class.getResource("/code/").getFile()).build(
				"/vue/style-01.ftl", 
				"D:/workspace/xw-boss-h5/src/views/"+req.getVueModelName()+"/"+req.getServiceName()+".vue",
				req);
		if(req.isFetchjs()) {
			if(req.getConfig().get表格列表()!=null)
			FileUtil.insertFile(listOpt(req), FileUtil.totalRowFile(fetchjs), fetchjs);
			if(req.getConfig().get新增()!=null)
			FileUtil.insertFile(addOpt(req), FileUtil.totalRowFile(fetchjs), fetchjs);
			if(req.getConfig().get删除()!=null)
			FileUtil.insertFile(deleteOpt(req), FileUtil.totalRowFile(fetchjs), fetchjs);
			if(req.getConfig().get编辑()!=null)
			FileUtil.insertFile(updateOpt(req), FileUtil.totalRowFile(fetchjs), fetchjs);
			if(req.getConfig().get详情()!=null)
			FileUtil.insertFile(getOpt(req), FileUtil.totalRowFile(fetchjs), fetchjs);
		}
		
	}
	
	public String getOpt(AntdMakerReq req) {
		return "    /**\r\n"
				+ "     * 获取详情"+req.getServiceName()+"服务\r\n"
				+ "     * @param {*} data \r\n"
				+ "     * @param {*} successful \r\n"
				+ "     * @param {*} fail \r\n"
				+ "     * @returns \r\n"
				+ "     */\r\n"
				+ "    static get"+StringUtils.capitalize(req.getServiceName())+"( id, successful, fail=null ){\r\n"
				+ "\r\n"
				+ "        return request({\r\n"
				+ "            url: '"+req.getConfig().get详情().getUrl()+"?"+req.getConfig().getKeyField()+"='+id,\r\n"
				+ "            method: '"+req.getConfig().get详情().getMethod()+"'\r\n"
				+ "        }).then(res=>{\r\n"
				+ "            successful(res)\r\n"
				+ "        }).catch(err=>{\r\n"
				+ "            if(fail){\r\n"
				+ "                fail(err)\r\n"
				+ "            }\r\n"
				+ "        })\r\n"
				+ "    }";
	}
	
	public String addOpt(AntdMakerReq req) {
		return "    /**\r\n"
				+ "     * 新增"+req.getServiceName()+"服务\r\n"
				+ "     * @param {*} data \r\n"
				+ "     * @param {*} successful \r\n"
				+ "     * @param {*} fail \r\n"
				+ "     * @returns \r\n"
				+ "     */\r\n"
				+ "    static add"+StringUtils.capitalize(req.getServiceName())+"( data, successful, fail=null ){\r\n"
				+ "\r\n"
				+ "        return request({\r\n"
				+ "            url: '"+req.getConfig().get新增().getUrl()+"',\r\n"
				+ "            method: '"+req.getConfig().get新增().getMethod()+"',\r\n"
				+ "            data,\r\n"
				+ "        }).then(res=>{\r\n"
				+ "            successful(res)\r\n"
				+ "        }).catch(err=>{\r\n"
				+ "            if(fail){\r\n"
				+ "                fail(err)\r\n"
				+ "            }\r\n"
				+ "        })\r\n"
				+ "    }";
	}
	
	public String listOpt(AntdMakerReq req) {
		return "    /**\r\n"
				+ "     * 获取"+req.getServiceName()+"列表服务\r\n"
				+ "     * @param {*} data \r\n"
				+ "     * @param {*} successful \r\n"
				+ "     * @param {*} fail \r\n"
				+ "     * @returns \r\n"
				+ "     */\r\n"
				+ "    static get"+StringUtils.capitalize(req.getServiceName())+"List( data, successful, fail=null ){\r\n"
				+ "\r\n"
				+ "        return request({\r\n"
				+ "            url: '"+req.getConfig().get表格列表().getUrl()+"',\r\n"
				+ "            method: '"+req.getConfig().get表格列表().getMethod()+"',\r\n"
				+ "            data,\r\n"
				+ "        }).then(res=>{\r\n"
				+ "            successful(res)\r\n"
				+ "        }).catch(err=>{\r\n"
				+ "            if(fail){\r\n"
				+ "                fail(err)\r\n"
				+ "            }\r\n"
				+ "        })\r\n"
				+ "    }";
	}
	
	public String deleteOpt(AntdMakerReq req) {
		return "    /**\r\n"
				+ "     * 删除"+req.getServiceName()+"服务\r\n"
				+ "     * @param {*} data \r\n"
				+ "     * @param {*} successful \r\n"
				+ "     * @param {*} fail \r\n"
				+ "     * @returns \r\n"
				+ "     */\r\n"
				+ "    static delete"+StringUtils.capitalize(req.getServiceName())+"( data, successful, fail=null ){\r\n"
				+ "\r\n"
				+ "        return request({\r\n"
				+ "            url: '"+req.getConfig().get删除().getUrl()+"',\r\n"
				+ "            method: '"+req.getConfig().get删除().getMethod()+"',\r\n"
				+ "            data,\r\n"
				+ "        }).then(res=>{\r\n"
				+ "            successful(res)\r\n"
				+ "        }).catch(err=>{\r\n"
				+ "            if(fail){\r\n"
				+ "                fail(err)\r\n"
				+ "            }\r\n"
				+ "        })\r\n"
				+ "    }";
	}
	public String updateOpt(AntdMakerReq req) {
		return "    /**\r\n"
				+ "     * 编辑"+req.getServiceName()+"服务\r\n"
				+ "     * @param {*} data \r\n"
				+ "     * @param {*} successful \r\n"
				+ "     * @param {*} fail \r\n"
				+ "     * @returns \r\n"
				+ "     */\r\n"
				+ "    static update"+StringUtils.capitalize(req.getServiceName())+"( data, successful, fail=null ){\r\n"
				+ "\r\n"
				+ "        return request({\r\n"
				+ "            url: '"+req.getConfig().get编辑().getUrl()+"',\r\n"
				+ "            method: '"+req.getConfig().get编辑().getMethod()+"',\r\n"
				+ "            data,\r\n"
				+ "        }).then(res=>{\r\n"
				+ "            successful(res)\r\n"
				+ "        }).catch(err=>{\r\n"
				+ "            if(fail){\r\n"
				+ "                fail(err)\r\n"
				+ "            }\r\n"
				+ "        })\r\n"
				+ "    }";
	}
}
